package com.bean;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Map.Entry;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.FileResourceLoader;

import com.excel.ExcelData;

/**
 *
 * @author zhoufang
 * @version v0.1 2019年7月9日 上午11:09:35  zhoufang
 */
public class JavaBeanParse {

	public static String JavaBeanContent = null;

	public static void parse() {
		String property = System.getProperty("user.dir");
		String filePath = property + File.separator + "src/main/java/com/bean/javaModel.txt";
		File file = new File(filePath);
		FileInputStream fis = null;
		BufferedReader reader = null;
		try {
			fis = new FileInputStream(file);
			reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
			StringBuilder sb = new StringBuilder();
			while (reader.ready()) {
				String str = reader.readLine();
				sb.append(str);
				sb.append("\r\n");
			}
			JavaBeanContent = sb.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void generateJava(String excelName,ExcelData excelData) {
//		parse();
		ClassBean classBean = new ClassBean();
		classBean.fileName = excelName;
		classBean.name = getClassName(excelName);
		classBean.desc = excelName+"---"+excelData.sheetName;
		Map<Integer, String> descMap = excelData.getDescMap();
		Map<Integer, String> nameMap = excelData.getNameMap();
		Map<Integer, String> typeMap = excelData.getTypeMap();
		for (Entry<Integer, String> entry : descMap.entrySet()) {
			if (excelData.ignoreIndex.contains(entry.getKey())) {
				continue;
			}
			FieldBean field = new FieldBean();
			field.desc = entry.getValue();
			field.name = nameMap.get(entry.getKey());
			field.type = typeMap.get(entry.getKey());
			field.bigName = getBigName(field.name);
			classBean.fieldList.add(field);
		}
		
		String root = System.getProperty("user.dir")+File.separator+"src\\main\\java\\com\\bean\\";
		FileResourceLoader resourceLoader = new FileResourceLoader(root,"UTF-8");
//		StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
		Configuration cfg = null;
		FileOutputStream fileOutputStream = null;
		try {
			cfg = Configuration.defaultConfiguration();
			GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
//			Template t = gt.getTemplate(JavaBeanContent);
			Template t = gt.getTemplate("javaModel.txt");
			t.binding("class", classBean);
			String content = t.render();

			String filePath = System.getProperty("user.dir") + File.separator + "src\\main\\java\\com\\bean\\model\\";
			String fileName = classBean.name + ".java";
			File file = new File(filePath + fileName);
			if (file.exists()) {
				file.delete();
			}
			fileOutputStream = new FileOutputStream(file);
			byte[] b = content.getBytes("UTF-8");
			fileOutputStream.write(b);
			fileOutputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	private static String getBigName(String name) {
		String first = name.substring(0, 1);
		String next = name.substring(1, name.length());
		return first.toUpperCase() + next;
	}

	private static String getClassName(String excelName) {
		String[] split = excelName.split("\\.");
		return "Dict" + getBigName(split[0]);
	}

}
